package model

import (
    "strconv"
    
    "gorm.io/gorm"
    
    "collecter/app/cache"
    "collecter/app/common/enum"
)

type Categorys struct {
    Id        int64  `json:"id,omitempty"`
    UserId    int64  `json:"user_id,omitempty"`
    Pid       int64  `json:"pid,omitempty"`
    CateName  string `json:"cate_name,omitempty"`
    Icon      string `json:"icon,omitempty"`
    Sort      int    `json:"sort,omitempty"`
    BaseModel
}

func (this *Categorys) AfterSave(tx *gorm.DB) error {
    this.deleteTreeCache()
    return nil
}

func (this *Categorys) AfterUpdate(tx *gorm.DB) error {
    this.deleteTreeCache()
    return nil
}

func (this *Categorys) AfterDelete(tx *gorm.DB) error {
    this.deleteTreeCache()
    return nil
}

func (this *Categorys) deleteTreeCache() {
    cacheKey := enum.USER_CATEGORY_TREE + strconv.FormatInt(this.UserId, 10)
    _ = cache.Cacher.Delete(cacheKey)
}
